/*
 * DigitalIO.h
 *
 *  Created on: Oct 28, 2011
 *      Author: Philip Rodriguez, Alfred Lee, Oliver Wing
 *
 *  Output data from A/D converter to digital IO
 */

#ifndef DIGITALIO_H_
#define DIGITALIO_H_

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <errno.h>
#include <sys/siginfo.h>
#include <sys/netmgr.h>
#include <sys/neutrino.h>
#include <sys/mman.h>
#include <hw/inout.h>
#include "types.h"

// The Digital IO addresses
#define DIO_CTRL_ADDRESS 		(0x28B)
#define PORTA_ADDRESS 			(0x288)
#define PORTB_ADDRESS 			(0x289)
#define PORTC_ADDRESS 			(0x28A)

// The directions of the Digital IO ports
#define PORTA_DIRECTION			(0b00010000)
#define PORTB_DIRECTION			(0b00000000)
#define PORTCH_DIRECTION		(0b00001000)
#define PORTCL_DIRECTION		(0b00000001)
#define PORTC_BEHAVIOR			(0b10000000)

// Signals of either a HIGH or LOW line
#define HIGH					(0xFF)
#define LOW						(0x00)

// Arbitrary test value for POST. Can be any byte
#define TEST_VAL				(0x55)

// handles to the Digital IO
extern uintptr_t dioControlHandle;
extern uintptr_t adDataHandle;
extern uintptr_t statusHandle;
extern uintptr_t readySignalHandle;

// The status byte from the QNX containing (currently) two flags
extern UINT8 statusByte;

// Initialize the settings for the Digital IO
void initDigitalIORegister(void);

// Quasi-POST test to verify proper setup of the DAQ output ports
bool POSTOutputDAQ(void);

// Send a signal to Freescale that QNX is ready to
// send the data from the conversion
void outputReadySignal(UINT8);

// Send the digital data to the Digital IO ports
void sendData(INT16);

// Connection error detected; can't hear Freescale.
#define STATUS_CONNECTION_ERROR			0b00000001
// Voltage out of bounds ( < -5V or > +5V ).
#define STATUS_VOLTAGE_WARNING			0b00000010
// Done reading A/D.
#define STATUS_CONVERSION_READY			0b00000100
// POST Failure.
#define STATUS_POST_SUCCESS				0b00001000

// Various status messages which will indicate an error
// through an LED to the user

// Output the current status to an LED
void OutputStatus(void);
// There is a problem with the connection
void RaiseConnectionError(void);
// There is no problem with the connection
void ClearConnectionError(void);
// A voltage has exceeded the [-5, +5] V range
void RaiseVoltageWarning(void);
// There is no problem with the voltage being out of range
void ClearVoltageWarning(void);
// The digital data from conversion is ready to send
void ConversionResultReady(void);
// The A/D conversion is being performed
void ConversionInProgress(void);
// The POSTs failed.
void RaisePOSTSuccess(void);
// The POSTs passed.
void ClearPOSTSuccess(void);

#endif /* DIGITALIO_H_ */
